import random
import urllib.request

class HtmlDownloader(object):

    def download(self,url):
        if url is None:
            return None

        response = urllib.request.urlopen(url)

        if response.getcode() != 200:
            return None
        return response.read()

    def disguiseDownloader(self,url):
        r=random.randint(1,5)
        print(r)
        if url is None:
            return None
        if r == 1:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'}
        if r == 2:
            headers = {
                # 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                # 'Accept-Encoding':'gzip, deflate, sdch, br',
                # 'Accept-Language':'zh-CN,zh;q=0.8',
                # 'Cache-Control':'no-cache',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
        if r == 3:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'}
        if r== 4:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
        if r == 5:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299'}
        req = urllib.request.Request(url=url, headers=headers)
        response = urllib.request.urlopen(req)
        #print('1',response.read())

        if response.getcode() != 200:
            return None
        return response.read()